package com.mercycrazy.recruit.filter;

import com.mercycrazy.recruit.user.SimpleAdminInfo;
import com.mercycrazy.recruit.user.UserInfo;

/**
 * <h1>使用 ThreadLocal 去单独存储每一个线程携带的 UserInfo 登录信息</h1>
 * 要及时的清理我们保存到 ThreadLocal 中的用户信息:
 * 1. 保证没有资源泄漏
 * 2. 保证线程在重用时, 不会出现数据混乱
 *
 * @author <a href="mailto:1443424326@qq.com">mercycrazy</a>
 * @since 2023-01-16
 */
public class AccessContext {

    private static final ThreadLocal<UserInfo> LOGIN_USER_INFO_THREAD_LOCAL = new ThreadLocal<>();

    private static final ThreadLocal<SimpleAdminInfo> LOGIN_ADMIN_INFO_THREAD_LOCAL = new ThreadLocal<>();

    private static final ThreadLocal<String> LOGIN_USER_PLATFORM_THREAD_LOCAL = new ThreadLocal<>();

    public static void setLoginUserInfo(UserInfo userInfo) {
        LOGIN_USER_INFO_THREAD_LOCAL.set(userInfo);
    }

    public static UserInfo getLoginUserInfo() {
        return LOGIN_USER_INFO_THREAD_LOCAL.get();
    }

    public static void setLoginPlatform(String platform) {
        LOGIN_USER_PLATFORM_THREAD_LOCAL.set(platform);
    }

    public static String getLoginPlatform() {
        return LOGIN_USER_PLATFORM_THREAD_LOCAL.get();
    }

     public static void setLoginAdminInfo(SimpleAdminInfo adminInfo) {
        LOGIN_ADMIN_INFO_THREAD_LOCAL.set(adminInfo);
     }

     public static SimpleAdminInfo getLoginAdminInfo() {
        return LOGIN_ADMIN_INFO_THREAD_LOCAL.get();
     }

    public static void clear() {
        LOGIN_USER_INFO_THREAD_LOCAL.remove();
        LOGIN_USER_PLATFORM_THREAD_LOCAL.remove();
        LOGIN_ADMIN_INFO_THREAD_LOCAL.remove();
    }
}
